home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2324 / 2324.xpi / chrome / sessionmanager.jar / content / sessionmanager / restore_prompt.js < prev    next >
Encoding:
Text File  |  2009-11-21  |  5.3 KB  |  143 lines

  1. gSessionManager.restorePrompt = function() {
  2.     this.onLoad(true);
  3.     this.onLoad = function() { };
  4.     this.onUnload = function() { };
  5.     this.onWindowClose = function() { };
  6.                 
  7.     // Set to not delete stored autosave name and time by default
  8.     var deletePrefs = false;
  9.  
  10.     // Don't try to encrypt backup file by default
  11.     this.delPref("_encrypt_file");
  12.     
  13.     // Don't recover by default
  14.     this.delPref("_recovering");
  15.     
  16.     // Default to user not selecting tabs
  17.     this.delPref("_chose_tabs");
  18.     
  19.     // default count variable
  20.     var countString = "";
  21.     
  22.     var session = null, backupFile = null, state = null, count = null;
  23.     var    screensize = screen.width + "x" + screen.height;
  24.             
  25.     // Get count from crashed session and prepare to save it.  Don't save it yet or it will show up in selection list.
  26.     var file = this.getProfileFile("sessionstore.js");
  27.     
  28.     // If file does not exist, try looking for SeaMonkey's sessionstore file
  29.     if (!file.exists()) {
  30.         file = this.getProfileFile("sessionstore.json");
  31.     }
  32.     
  33.     if (file.exists())
  34.     {
  35.         try {
  36.             var name = this.getFormattedName("", new Date(file.lastModifiedTime), this._string("crashed_session"));
  37.             state = this.readFile(file);
  38.             count = this.getCount(state);
  39.             session = gSessionManager.nameState("timestamp=" + file.lastModifiedTime + "\nautosave=false\tcount=" + count.windows + "/" + count.tabs + "\tgroup=" + gSessionManager._string("backup_sessions") + "\tscreensize=" + screensize + "\n" + state, name);
  40.             backupFile = this.getSessionDir(this.mBackupSessionName, true);
  41.             
  42.             if (count.windows && count.tabs) countString = count.windows + "," + count.tabs;
  43.         }
  44.         catch(ex) { 
  45.             this.logError(ex); 
  46.         }
  47.     }
  48.     
  49.     var params = window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  50.     params.SetInt(0, 0);
  51.             
  52.     var values = { name: "*", addCurrentSession: true, ignorable: false, count: countString }
  53.     var fileName = (location.search != "?cancel")?(this.prompt(this._string("recover_session"), this._string("recover_session_ok"), values)?values.name:""):"";
  54.     if (fileName != "*")
  55.     {
  56.         if (fileName)
  57.         {
  58.             this.setPref("_recovering", fileName);
  59.         }
  60.         else if (!this.getPref("save_window_list", false))
  61.         {
  62.             this.clearUndoData("window", true);
  63.         }
  64.         params.SetInt(0, 1); // don't recover the crashed session
  65.     }
  66.     
  67.     this.mPref_encrypt_sessions = this.getPref("encrypt_sessions", false);
  68.     // actually save the crashed session
  69.     if (session && backupFile) {
  70.         this.writeFile(backupFile, session);
  71.         if (this.mPref_encrypt_sessions) this.setPref("_encrypt_file", backupFile.leafName);
  72.     }
  73.     
  74.     // If user chose to prompt for tabs and selected a filename
  75.     if (fileName && values.choseTabs) {
  76.         // if recovering current session, recover it from our backup file
  77.         if (fileName == "*") {
  78.             fileName = backupFile.leafName;
  79.             params.SetInt(0, 1); // don't recover the crashed session
  80.             this.setPref("_recovering", fileName);
  81.         }
  82.         this.setPref("_chose_tabs", true);
  83.     }
  84.         
  85.     var autosave_values = this.getPref("_autosave_values", "").split("\n");
  86.     var autosave_name = autosave_values[0];
  87.     if (autosave_name)
  88.     {
  89.         // if not recovering last session (does not including recovering last session, but selecting tabs)
  90.         if (fileName != "*")
  91.         {
  92.             // Get name of chosen session
  93.             var chosen_name = null;
  94.             if (/^(\[SessionManager v2\])(?:\nname=(.*))?/m.test(this.readSessionFile(this.getSessionDir(fileName), true))) {
  95.                 chosen_name = RegExp.$2;
  96.             }
  97.             
  98.             // not recovering autosave session or current session (selecting tabs), save the autosave session first
  99.             if (values.choseTabs || ((chosen_name != autosave_name) && (fileName != backupFile.leafName)))
  100.             {
  101.                 // delete autosave preferences
  102.                 deletePrefs = true;
  103.                 
  104.                 this.log("Saving crashed autosave session " + autosave_name, "DATA");
  105.                 var temp_state = this.readFile(file);
  106.                 // encrypt if encryption enabled
  107.                 if (this.mPref_encrypt_sessions) {
  108.                     this.mPref_encrypted_only = this.getPref("encrypted_only", false);
  109.                     temp_state = this.decryptEncryptByPreference(temp_state);
  110.                 }
  111.                 
  112.                 if (temp_state) {
  113.                     var autosave_time = isNaN(autosave_values[2]) ? 0 : autosave_values[2];
  114.                     var autosave_state = gSessionManager.nameState("timestamp=" + file.lastModifiedTime + "\nautosave=session/" + autosave_time +
  115.                                                                    "\tcount=" + count.windows + "/" + count.tabs + (autosave_values[1] ? ("\tgroup=" + autosave_values[1]) : "") +
  116.                                                                    "\tscreensize=" + screensize + "\n" + temp_state, autosave_name);
  117.                     this.writeFile(this.getSessionDir(this.makeFileName(autosave_name)), autosave_state);
  118.                 }
  119.             }
  120.             // choose to recover autosave session so just recover last session
  121.             else 
  122.             {
  123.                 // we could delete the autosave preferences here, but it doesn't matter (actually it saves us from saving prefs.js file again)
  124.                 this.delPref("_recovering");
  125.                 params.SetInt(0, 0);
  126.             }
  127.         }
  128.     }
  129.     
  130.     // delete autosave preferences and save preference file
  131.     if (deletePrefs) {
  132.         this.delPref("_autosave_values");
  133.         // do this via a preference so we don't save twice in case user loads a different auto save sessions
  134.         this.setPref("_save_prefs", true);  
  135.     }
  136.     
  137.     // Don't prompt for a session again if user cancels crash prompt
  138.     this.setPref("_no_prompt_for_session", true);
  139. };
  140.         
  141. gSessionManager.restorePrompt();
  142. window.close();
  143.